/**
 * 
 */
package com.skood.security.service.impl;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.skood.common.ExecuteResult;
import com.skood.common.dao.CommonDAO;
import com.skood.common.service.AbstractService;
import com.skood.security.dao.RoleDAO;
import com.skood.security.domain.Role;
import com.skood.security.service.RoleService;

/**
 * @author keege
 * @date 2013-12-7
 */
@Service("roleService")
public class RoleServiceImpl extends AbstractService<Role> implements RoleService {
	private static Logger LOG = LoggerFactory.getLogger(RoleServiceImpl.class);
	
	@Resource(name = "roleDAO")
	private RoleDAO roleDAO;

	@Override
	public ExecuteResult<Role> createRole(Role entityBean, Long[] resourceIds) {
		// TODO Auto-generated method stub
		ExecuteResult<Role> executeResult = new ExecuteResult<Role>();
		if(entityBean == null || StringUtils.isEmpty(entityBean.getName())){
			executeResult.addErrorMessages("角色名称不能为空");
			return executeResult;
		}
		Role dbRole = roleDAO.findRoleByName(entityBean.getName());
		if(dbRole != null){
			executeResult.addErrorMessages("角色名称已经存在");
			return executeResult;
		}
		try{
			Map<String,Object> parameters = new HashMap<String,Object>();
			parameters.put("userId", "1");
			parameters.put("userName", "keege");
			roleDAO.save(entityBean, parameters);
			dbRole = roleDAO.findRoleByName(entityBean.getName());
			roleDAO.saveRoleResource(dbRole.getId(), resourceIds);
		}catch(RuntimeException e){
			LOG.error("保存角色执行失败{}",e.getMessage());
			throw e ;
		}
		executeResult.setResult(dbRole);
		return executeResult;
	}

	@Override
	protected CommonDAO<Role> getDAO() {
		// TODO Auto-generated method stub
		return roleDAO;
	}

}
